import Vue from 'vue'
export default{
    namespaced:true,
    state(){
        return{
            cartList:[
                {
                    proNum:1,
                    displayName: "双鱼座-项链-生辰石-18K玫瑰金",
                    proPrice: 4999,
                    listImgs: [{listImgpath: "https://img.roseonly.com.cn/www/2021-09-03/1630632222899-3153777531763602.png"},],
                    id:3136 ,
                    proSeries: "星座系列",
                    ischecked: true,
                    numb:2,
                },
                {
                    proNum:2,
                    displayName: "双鱼座-项链-生辰石-18K玫瑰金",
                    proPrice: 5999,
                    listImgs:[{listImgpath: "https://img.roseonly.com.cn/www/2022-02-10/1644503844191-5565600267003324.png"},],
                    id:3138,
                    proSeries: "星座系列",
                    prosort:"b",
                    ischecked: true,
                    numb:1,
                }
            ]
        }
    },
    getters: {
        allRes: state => {
           let allchecked=true
           let allprice=0
           let allcheckedcart=[]
           state.cartList.forEach(obj=>{
               if(obj.ischecked){
                allprice+=obj.proPrice*obj.numb*100
                allcheckedcart.push(obj)
               }
               if(!obj.ischecked){
                allchecked=false
               }
           })
           console.log(allcheckedcart);
           return {allchecked,allprice,allcheckedcart}
        }
    },
    mutations: {
        allcheckedmut(state, bool) {
            state.cartList.forEach(obj=>{
                obj.ischecked=bool
            })
        },
        addcartlistmut(state,obj){
            console.log(obj);
            let {cartList}=state
            for(var i=0;i<cartList.lebgth;i++){
                if(cartList[i].id==obj.id){
                    cartList[i].numb++
                    return
                }
            }
            Vue.set(obj,'numb',1);
            Vue.set(obj,'ischecked',true)
            state.cartList.push(obj)
            console.log(state.cartList);

        },
        delcartmut(state,index){
            state.cartList.splice(index,1)
        }
    }
}